package cn.stylefeng.guns.modular.business.frequency.siterelation.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.exception.ServiceException;
import cn.stylefeng.guns.core.factory.PageFactory;
import cn.stylefeng.guns.core.pojo.page.PageResult;
import cn.stylefeng.guns.core.pojo.response.ResponseData;
import cn.stylefeng.guns.modular.business.frequency.siterelation.entity.SiteRelation;
import cn.stylefeng.guns.modular.business.frequency.siterelation.enums.BranchRelationExceptionEnum;
import cn.stylefeng.guns.modular.business.frequency.siterelation.mapper.SiteRelationMapper;
import cn.stylefeng.guns.modular.business.frequency.siterelation.model.param.SiteRelationParam;
import cn.stylefeng.guns.modular.business.frequency.siterelation.service.SiteRelationService;
import cn.stylefeng.guns.modular.business.util.BusinessUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 网点人员关联表 服务实现类
 *
 * @author yujie
 * @date 2023/05/10 16:16
 */
@Service
public class SiteRelationServiceImpl extends ServiceImpl<SiteRelationMapper, SiteRelation> implements SiteRelationService {

    @Autowired
    private BusinessUtil businessUtil;

    @Override
    public PageResult<SiteRelation> page(SiteRelationParam siteRelationParam) {

        // 构造条件
        LambdaQueryWrapper<SiteRelation> queryWrapper = new LambdaQueryWrapper<>();

        // 查询分页结果
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public List<SiteRelation> list(SiteRelationParam siteRelationParam) {

        // 构造条件
        LambdaQueryWrapper<SiteRelation> queryWrapper = new LambdaQueryWrapper<>();

        return this.list(queryWrapper);
    }

    @Override
    public void add(SiteRelationParam siteRelationParam) {

        // 将dto转为实体
        SiteRelation branchRelation = new SiteRelation();
        BeanUtil.copyProperties(siteRelationParam, branchRelation);

        this.save(branchRelation);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(SiteRelationParam siteRelationParam) {
        this.removeById(siteRelationParam.getId());
    }

    @Override
    public void edit(SiteRelationParam siteRelationParam) {

        // 根据id查询实体
        SiteRelation branchRelation = this.queryBranchRelation(siteRelationParam);

        // 请求参数转化为实体
        BeanUtil.copyProperties(siteRelationParam, branchRelation);

        this.updateById(branchRelation);
    }

    @Override
    public SiteRelation detail(SiteRelationParam siteRelationParam) {
        return this.queryBranchRelation(siteRelationParam);
    }

    @Override
    public ResponseData bindUser(SiteRelationParam siteRelationParam) {
        SiteRelation relation = this.getOne(new QueryWrapper<SiteRelation>().lambda().eq(SiteRelation::getUserId, siteRelationParam.getUserId()));
        if (relation!=null){
            relation.setSiteId(siteRelationParam.getSiteId());
            this.updateById(relation);
        }else {
            relation = new SiteRelation();
            relation.setUserId(siteRelationParam.getUserId());
            relation.setSiteId(siteRelationParam.getSiteId());
            relation.setTenantId(businessUtil.getTenantId());
            this.save(relation);
        }
        return ResponseData.andaSuccess();
    }


    /**
     * 获取网点人员关联表
     *
     * @author yujie
     * @since 2023/05/10 16:16
     */
    private SiteRelation queryBranchRelation(SiteRelationParam siteRelationParam) {
        SiteRelation branchRelation = this.getById(siteRelationParam.getId());
        if (ObjectUtil.isEmpty(branchRelation)) {
            throw new ServiceException(BranchRelationExceptionEnum.NOT_EXISTED);
        }
        return branchRelation;
    }
}